package com.zzyl.nursing.service.impl;

import java.util.List;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDTO;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-30
 */
@Service
@Transactional
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        // 1.根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        // 2.根据护理计划id查询关联的所有护理项目
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(Long.valueOf(id));

        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }


    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDTO dto)
    {
        //1、保存护理计划基本信息
        //对象属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insert(nursingPlan);

        //2、批量保存护理计划项目关联表
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();

        //补全护理计划id
        projectPlans.forEach(item -> item.setPlanId(Long.valueOf(nursingPlan.getId())));

        //批量保存护理计划项目关联表
       Long result = nursingProjectPlanMapper.batchSave(projectPlans);
        return result>0 ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDTO dto)
    {
        // 1、获取护理计划ID
        Long planId = dto.getId();
        if (planId == null) {
            throw new IllegalArgumentException("护理计划ID不能为空");
        }

        // 2、更新护理项目信息
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();

        if (projectPlans != null && !projectPlans.isEmpty()){
            // 2.1 根据护理计划id删除护理计划关联表中的护理项目
            nursingProjectPlanMapper.deleteByPlanId(planId);

            // 2.2 补全护理计划id并批量保存
            projectPlans.forEach(item -> item.setPlanId(planId));
            Long result = nursingProjectPlanMapper.batchSave(projectPlans);
            
            if (result <= 0) {
                return 0; // 保存失败
            }
        }

        // 3、更新基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setId(Math.toIntExact(planId)); // 确保ID被正确设置
        
        return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
//    @Override
//    public int deleteNursingPlanByIds(Integer[] ids)
//    {
//        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
//    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        // 删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        // 删除护理计划
        return removeById(id) ? 1 : 0;
    }
}
